﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace PPAP.Web.Master
{
    public partial class Signature : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["username"] != null)
                {
                    string path = HttpContext.Current.Request.MapPath("../Part/image/");
                    if (File.Exists(path + Request.QueryString["username"].ToString() + ".png"))
                    {
                        Image1.ImageUrl = "../Part/image/" + Request.QueryString["username"].ToString() + ".png";
                    }
                    else
                    {
                        Image1.ImageUrl = "../Part/image/u76.jpg";
                    }
                }
            }
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLoad_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["username"] != null )
            {
                string FileName = file.FileName;
                string path = HttpContext.Current.Request.MapPath("../Part/image/");
                
                if (!string.IsNullOrEmpty(FileName))
                {
                    string extension = System.IO.Path.GetExtension(FileName);//获取文件类型后缀名
                    if ((extension.ToLower() == ".png"))
                    {
                        int fileSize = file.PostedFile.ContentLength;
                        if (fileSize == 0)
                        {
                            base.AlertMessage("The file does not exist！");
                        }
                        else
                        {
                            if (File.Exists(path + Request.QueryString["username"].ToString() + ".png"))
                            {
                                File.Delete(path + Request.QueryString["username"].ToString() + ".png");
                            }
                            if (true)//限制大小为1M
                            {
                                file.SaveAs(path + Request.QueryString["username"].ToString() + ".png");  //将文件上传到服务器
                                AlertMessage("Uploaded successfully");
                                if (File.Exists(path + Request.QueryString["username"].ToString() + ".png"))
                                {
                                    Image1.ImageUrl = "../Part/image/" + Request.QueryString["username"].ToString() + ".png";
                                }
                            }
                        }
                    }
                    else
                    {
                        AlertMessage("Only png file supported!");
                    }
                }
                else
                {
                    AlertMessage("Please select  file.");
                }
            }
        }
    }
}